/*
 * TheMapper.cpp
 *
 *  Created on: Jan 6, 2014
 *      Author: nick
 */

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include "MapperGlobals.h"
#include "MapperTile.h"
#include "MapperLevel.h"
#include "Camera.h"

#include <string>
#include <iostream>

using namespace std;

SDL_Surface *load_image() {
	//The image that's loaded
	SDL_Surface* loadedImage = NULL;

	//The optimized surface that will be used
	SDL_Surface* optimizedImage = NULL;

	//Load the image
	loadedImage = IMG_Load(TILE_FILE.c_str());

	//If the image loaded
	if (loadedImage != NULL) {
		//Create an optimized surface
		optimizedImage = SDL_DisplayFormat(loadedImage);

		//Free the old surface
		SDL_FreeSurface(loadedImage);

		//If the surface was optimized
		if (optimizedImage != NULL) {
			//Color key surface
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY,
					SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
		}
	}

	//Return the optimized surface
	return optimizedImage;
}

bool Open_Files() {

	//Load the tile sheet
	tileSheet = load_image();

	//If there was a problem in loading the tiles
	if (tileSheet == NULL) {
		cout << "Error loading the filesheet" << endl;
		return false;
	}

	//If everything loaded fine
	return true;
}

bool init() {
	//Initialize all SDL subsystems
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		return false;
	}

	//Set up the screen
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
			SDL_SWSURFACE);

	//If there was an error in setting up the screen
	if (screen == NULL) {
		return false;
	}

	//Set the window caption
	SDL_WM_SetCaption("The Mapper", NULL);

	//If everything initialized fine
	return true;
}

void clean_up(Tile *tiles[]) {
	//Free the surfaces
	SDL_FreeSurface(dot);
	SDL_FreeSurface(tileSheet);

	//Free the tiles
	for (int t = 0; t < TOTAL_TILES; t++) {
		delete tiles[t];
	}

	//Quit SDL
	SDL_Quit();
}

void Draw_Menu() {
	SDL_FillRect(screen, &menu, SDL_MapRGB(screen->format, 255, 255, 255));
}

int main(int argc, char* args[]) {
	//Quit flag
	bool quit = false;

	//The tiles that will be used
	Tile *tiles[TOTAL_TILES];

	Camera camera(0, 0, SCREEN_WIDTH - (2 * TILE_WIDTH), (int) SCREEN_HEIGHT,
			-1);

	//Initialize
	if (init() == false) {
		return 1;
	}

	//Load the files
	if (!Open_Files()) {
		return 1;
	}

	//Clip the tile sheet
	clip_tiles();

	//Set the tiles
	if (!set_tiles(tiles)) {
		return 1;
	}

	//While the user hasn't quit960
	while (quit == false) {

		//While there's events to handle
		while (SDL_PollEvent(&event)) {

			camera.handleInput();

			//If the user has Xed out the window
			if (event.type == SDL_QUIT) {
				//Quit the program
				quit = true;
			}
		}

		camera.move();

		//Show the tiles
		for (int t = 0; t < TOTAL_TILES; t++) {
			if (camera.isWithin(tiles[t]->getBox())) {
				//Show the tile
				tiles[t]->show(camera.getX(), camera.getY(), tileSheet, screen,
						tiles[t]->getBox());
			}
		}

		Draw_Menu();

		//Update the screen
		if (SDL_Flip(screen) == -1) {
			return 1;
		}

	}

	//Clean up
	clean_up(tiles);

	return 0;
}

